public class HospitalChangeExt {
    String choice = null;
    public String msg{get;set;}
    public Approval.ProcessResult result;
    public  List<Hospital__c> hospital = [select id,approvaltype__c,name,ownerid from Hospital__c 
                                  where id=: System.currentPageReference().getParameters().get('id')];        
    list<hospital_approve__c> hal=[select id from hospital_approve__c where hospital__c=:System.currentPageReference().getParameters().get('id')];
   public List<Hospital_approve__c> target=new List<Hospital_approve__c>();
    public void checkha(){
         if(hal.size()==0){
                hospital_approve__c ha=new hospital_approve__c(name=hospital[0].name,hospital__c=System.currentPageReference().getParameters().get('id'),owner__c=hospital[0].ownerid);
                insert ha;
             }
        target = [select id,Name, approvalstatus__c,owner__c, class__c, approvaltype__c, Active__c, 
                                classchangeto__c, ownerchangeto__c, namechangeto__c, comments__c from Hospital_approve__c 
                                where hospital__c=: System.currentPageReference().getParameters().get('id')];
     }
    String hosId = System.currentPageReference().getParameters().get('id');
    public Hospital__c ChangeOwnerHos
    {
        get{
            if(ChangeOwnerHos == null)
                ChangeOwnerHos = new Hospital__c();
             return ChangeOwnerHos;
        }
        set;
    }
                      
   
                      
    public Hospital_approve__c hos {
        get{
            if (hos==null)
                if(target.size() > 0)
                    hos = target[0];
                else
                    hos = null;
            return hos;
        }
        set;
    }
    
    public List<SelectOption> getItems() 
    { 
        List<SelectOption> options = new List<SelectOption>();
        //options.add(new SelectOption('ACTIVATE', '新增医院'));
        options.add(new SelectOption('DELETE','删除医院')); 
        options.add(new SelectOption('RENAME','医院重命名')); 
        options.add(new SelectOption('CLASS','改变医院级别'));
        List<User> usr = [select Id, title__c from User where Id=:UserInfo.getUserId()];
        if(usr[0].title__c != 'Sales Rep')
            options.add(new SelectOption('OWNER','改变医院及相关医护人员的MSR')); 
        return options;
    }
    

    
    public PageReference Submit() {
    if(hos.approvalstatus__c=='审批通过但未生效'||hos.approvalstatus__c=='等待审批'){
        msg = '您还有尚未生效的审批申请，生效前不可再次提交审批。';
        return null;
    } 
    
    try{
            if (hos.Active__c=='是'){
            
            if (choice == 'RENAME' && hos.namechangeto__c == hos.Name)
                    hos.addError('重命名失败：名字未改变');
            else if(choice == 'CLASS' && hos.classchangeto__c == hos.class__c)
                    hos.addError('改变级别失败：级别未改变');
            else if(choice == 'OWNER' && hos.ownerchangeto__c == ChangeOwnerHos.Ownerid)
                hos.addError('改变所有人失败：所有人未改变');
            else{
                update hos;
               update hospital[0];
                // Create an approval request for the Hospital__c  
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setObjectId(target[0].id);
                //req1.setNextApproverIds(new Id[] {hos.approver__c});
                // Submit the approval request for the Hospital__c   
                result = Approval.process(req1);
                
                return new ApexPages.StandardController(new Hospital__c(Id = hosId)).view();}
              
             

            }
        
        else if(hos.Active__c == '否'){                    //&& hos.approvaltype__c == '新增医院'
            update hos;
            
            // Create an approval request for the Hospital__c  
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(target[0].id);
           // req1.setNextApproverIds(new Id[] {hos.approver__c}); 
            // Submit the approval request for the Hospital__c   
            result = Approval.process(req1);
            
            return new ApexPages.StandardController(new Hospital__c(Id = hosId)).view();
        }
        else if(choice == 'DELETE' || choice == 'RENAME' || choice == 'CLASS' || choice == 'OWNER')
            hos.addError('操作失败：该医院已被删除');
        return null;
            } catch (Exception e) {
                  msg='您没有可提交审批的经理，请联系管理员确认经理信息。';
                  hos.approvaltype__c = null;
                  hospital[0].approvaltype__c = null;
                  hos.approvaltype__c = null;
                  hos.classchangeto__c = null;
                  hos.namechangeto__c = null;
                  hos.ownerchangeto__c = null;
                  hos.comments__c = null;
                  hos.approvalstatus__c=null;
                  hospital[0].comments__c = null;
                  update hospital[0];
                  update hos;
                  return null;
                }
    }
    
    public PageReference Cancel() {
        return new ApexPages.StandardController(new Hospital__c(Id = hosId)).view();
    }
    
    public PageReference next() {
        ChangeOwnerHos = hospital[0];
        if(hos.approvalstatus__c=='审批通过但未生效'||hos.approvalstatus__c=='等待审批'){
        msg = '您还有尚未生效的审批申请，生效前不可再次提交审批。';
        return null;
         }
        if(choice == 'RENAME'){
            hos.approvaltype__c = '医院重命名';
            hospital[0].approvaltype__c = '医院重命名';
           // update hospital[0];
            return Page.HospitalChangeName;
        }else if(choice == 'CLASS'){
            hos.approvaltype__c = '改变医院级别';
            hospital[0].approvaltype__c = '改变医院等级';
          //  update hospital[0];
            return Page.HospitalChangeClass;
        }else if(choice == 'DELETE'){
            hos.approvaltype__c = '删除医院';
            hospital[0].approvaltype__c = '删除医院';
          //  update hospital[0];
            return Page.HospitalDelete;
        }else if(choice == 'OWNER'){
            hos.approvaltype__c = '改变MSR';
            hospital[0].approvaltype__c = '改变MSR';
         //  update hospital[0];
            return Page.HospitalChangeOwner;
        }
        
        return null;
    }
                   
    public String getChoice() {
        return choice;
    }
      
    public void setChoice(String choice) { this.choice = choice; }
    
    static testMethod void HospitalChangeExtTest()
    {

        Profile p  = [select id from profile where name = 'System Administrator' or name =: '系统管理员'];
        User u = new User (alias = 'adf', email = 'shddduaicao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles',username='stssssdf@testorg1.com');
        insert u;
        
        
        User u1 = new User (alias = 'standt', email = 'shuaiaaacao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', ManagerId = u.id ,username='standdddarduser@testorg1.com');
        insert u1;

    System.runAs(u1) { 
    
        HospitalChangeExt testcase = new HospitalChangeExt();
        
        Hospital__c hos1 = new Hospital__c(Name = 'a',Nickname__c = 'a',Active__c = true,approvaltype__c = '医院重命名');
        insert hos1;
       
        testcase.getItems();
        testcase.Cancel();
        testcase.getChoice();
       
        
        hospital_approve__c hal=[select id,Active__c,namechangeto__c,owner__c,approvaltype__c,ownerchangeto__c, Name,approvalstatus__c from hospital_approve__c where hospital__c=:hos1.id];
        system.assert(hal<>null);
        
        test.setCurrentPageReference(Page.HospitalChangeName);
        System.currentPageReference().getParameters().put('id', hos1.Id); 
        testcase.hospital.add(hos1);
       
        testcase.target.add(hal);
        testcase.choice = 'OWNER';
       
        hos1.namechangeto__c ='b';// hos1.Name;
        hos1.approvalstatus__c='';
        
        testcase.hospital.get(0).approvaltype__c = '改变MSR';

        testcase.Submit();
        testcase.next();
        testcase.Cancel();
        
        
        testcase.choice = 'RENAME';
        testcase.hospital.get(0).approvaltype__c = '医院重命名';
       testcase.getChoice();
        hos1.namechangeto__c ='b';// hos1.Name;
        hos1.approvalstatus__c='';
        testcase.next();
        testcase.Cancel();
        
       testcase.choice = 'DELETE';
       testcase.hospital.get(0).approvaltype__c = '删除医院';
       testcase.getChoice();
        hos1.namechangeto__c ='b';// hos1.Name;
        hos1.approvalstatus__c='';
        testcase.next();
        testcase.Cancel();
        
       testcase.choice = 'CLASS';
       testcase.hospital.get(0).approvaltype__c = '改变医院等级';
       testcase.getChoice();
        hos1.namechangeto__c ='b';// hos1.Name;
        hos1.approvalstatus__c='';
        testcase.next();
        testcase.Cancel();
        testcase.setChoice('RENAME');
        
        
        testcase.hospital.get(0).Active__c = false;
        testcase.getItems();
        testcase.Cancel();
        testcase.getChoice();
        testcase.Submit();
        
       }
    }     
}